package com.mycompany.app.common.scheduler.quartz;

import java.util.Calendar;
import java.util.Map;

import org.springframework.util.MethodInvoker;

import com.mycompany.app.common.scheduler.model.DaemonJobTrigger.IntervalType;
import com.mycompany.app.util.SpringUtils;

public class QuartzUtils {
    protected static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(QuartzUtils.class);

    public static String getCronExpression(IntervalType type, int seconds,
            int minutes, int hours, int dayOfMonth, int month, int dayOfWeek,
            int year) {

        StringBuffer sb = new StringBuffer();
        String time = seconds + " " + minutes + " " + hours + " ";
        sb.append(time);

        if (type == IntervalType.ONLY_ONCE) {
            Calendar curr = Calendar.getInstance();
            // set current day as default day.
            if (dayOfMonth == 0)
                sb.append(curr.get(Calendar.DAY_OF_MONTH));
            else
                sb.append(dayOfMonth);
            sb.append(" ");

            // set current month as deafault month.
            if (month == 0)
                sb.append(curr.get(Calendar.MONTH) + 1);
            else
                sb.append(month);
            sb.append(" ");

            sb.append("? ");

            // set current year as default year.
            if (year == 0)
                sb.append(curr.get(Calendar.YEAR));
            else
                sb.append(year);
        }

        else if (type == IntervalType.DAILY) {
            sb.append("* * ? *");
        }

        else if (type == IntervalType.WEEKLY) {
            sb.append("? * ");
            sb.append(dayOfWeek);
            sb.append(" *");
        }

        else if (type == IntervalType.MONTHLY) {
            if (dayOfMonth > 0)
                sb.append(dayOfMonth);
            else {
                // sb.append(-dayOfMonth);
                sb.append("L");
            }
            sb.append(" * ? *");
        }

        else if (type == IntervalType.YEARLY) {
            if (dayOfMonth > 0)
                sb.append(dayOfMonth);
            else {
                // /sb.append(-dayOfMonth);
                sb.append("L");
            }
            sb.append(" ");
            sb.append(month);
            sb.append(" ? *");
        }

        else
            throw new IllegalArgumentException(
                    "Illegal interval type, cant get cron expression.");

        if (log.isDebugEnabled())
            log.debug("IntervalType: " + type.name() + ". Cron expression "
                    + sb.toString());

        return sb.toString();
    }

    @SuppressWarnings("unchecked")
    public static MethodInvoker getDaemonJobImpl(String daemonJobName) {

        Map<String, MethodInvoker> daemonJobMap = (Map<String, MethodInvoker>) SpringUtils
                .getApplicationContext().getBean(QuartzConstant.DAEMON_JOB_MAP);

        return daemonJobMap.get(daemonJobName);

    }
}
